<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="_header_include::frag(~{::title},~{},~{})">
    <title>后台管理系统</title>
</head>
<body>
<div id="maindiv" class="maindiv mainPadding" style="display: none;">    
    <el-breadcrumb separator="/" class="elBreadcrumb marginB20" v-if="isElBreadcrumb">
        <el-breadcrumb-item>Home</el-breadcrumb-item>
        <el-breadcrumb-item>数据报表</el-breadcrumb-item>
        <el-breadcrumb-item>电销业绩表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row class="marginB20">
        <el-button type="primary" @click="searchMonth">月统计</el-button>
        <el-button type="success" @click="searchSeason">季统计</el-button>
        <el-button type="warning" @click="searchYear">年统计</el-button> 
         <!-- <shiro:hasPermission name="statistics:trafficCallResource:export"> -->
        <el-button type="danger" @click="exportRs">导出Excel报表</el-button>   
         <!-- </shiro:hasPermission> -->
        <el-button type="primary" v-if="!isDisabledUserId" @click="goBack" style="float: right;">返回</el-button>
    </el-row>
    <div class="mainSearchBg">        
        <div class="mainSearchFormBox">
           <el-form :inline="true" :model="form" class="demo-form-inline mainSearchForm" ref="form">
                <el-form-item label="统计日期：">
                    <el-date-picker format="yyyy-MM-dd 00:00" v-model="form.startTime" type="date" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="—" class="widthauto">
                    <el-date-picker format="yyyy-MM-dd 23:59" v-model="form.endTime" type="date" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="事业部：">  
                    <el-select v-model="form.teleDeptId" placeholder="全部" style="width: 100%;" clearable filterable @change="curTelGroupSale" :disabled="isDisabled">
                        <el-option
                            v-for="item in deptList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="电销组：">  
                    <el-select v-model="form.teleGroupId" placeholder="全部" style="width: 100%;" clearable filterable @change="curTelSale" :disabled="isDisabledOrgId">
                        <el-option
                            v-for="item in orgTeleArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="电销顾问:">  
                    <el-select v-model="form.teleSaleId" placeholder="全部" style="width: 100%;" clearable filterable :disabled="isDisabledUserId">
                        <el-option
                            v-for="item in busSaleArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="资源类别：">
                    <el-select v-model="form.category" placeholder="资源类别" clearable filterable>
                        <el-option
                            v-for="item in clueCategoryList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="搜索词：" prop="searchText">
                    <el-input v-model="form.searchText" placeholder="输入查询内容" class="input-with-select"></el-input>
                </el-form-item>
                <div class="mainSearchBtnBox">
                    <el-button class="searchBtn" icon="el-icon-search" type="primary" @click="searchTable(1)" :disabled="loading">搜索</el-button>
                </div>  
            </el-form>
        </div>
        <el-row>
            <el-table 
                ref="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                border
                :data="tableData"
                v-loading="loading"
                :cell-style="centerStyle"
                :header-cell-style="centerStyle"
                :span-method="arraySpanMethod">
                <el-table-column type="index" label="序号" width="55" :index="indexFun"></el-table-column>
                <el-table-column prop="userName" label="电销顾问">
                    <template slot-scope="scope">
                        <span size="small" v-if="scope.row.teleSaleId!='99999'">{{scope.row.userName}}</span>
                        <span v-else>{{scope.row.userName}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="culeNum" label="首次分配资源数"></el-table-column>
                <el-table-column prop="firstVisitNum" label="首访数"></el-table-column>
                <el-table-column prop="signNum" label="签约数"></el-table-column>
                <el-table-column prop="visitRate" label="资源来访率"></el-table-column>
                <el-table-column prop="signRate" label="签约率"></el-table-column>
                <el-table-column prop="achievement" label="业绩金额" :formatter="format_number"></el-table-column>
                <el-table-column prop="drinkAchievement" label="(饮品)业绩金额" :formatter="format_number"></el-table-column>
                <el-table-column prop="nonDrinkAchievement" label="(非饮品)业绩金额" :formatter="format_number"></el-table-column>
                <el-table-column prop="signAmount" label="签约单笔" :formatter="format_number"></el-table-column>
                <el-table-column prop="resourceEfficiency" label="资源效率" :formatter="format_number"></el-table-column>
                <el-table-column prop="depositAmount" label="定金量"></el-table-column>
                <el-table-column prop="tailAmount" label="尾款量"></el-table-column>
                <el-table-column prop="depositRatio" label="定金占比(首次)" :formatter="format_number"></el-table-column>
                <el-table-column prop="tailRecoveryRate" label="尾款回收率" :formatter="format_number"></el-table-column>
            </el-table>
            <table-pagination :pager="pager" @change="searchTable()"></table-pagination>
        </el-row>
    </div>
</div>
<!-- import common js-->
<div th:include="_footer_include :: #jsLib"></div>
</body>
<style>
    .el-table tr:nth-of-type(1) td:nth-of-type(n+1) {font-weight: bold;}
</style>
<script th:inline="javascript">
    var parentParam=[[${baseQueryDto}]]; // 跳转参数
    var deptList=[[${deptList}]];// 事业部
    var clueCategoryList=[[${clueCategoryList}]];// 资源类别
    var curDeptId =[[${deptId}]]; // 当前用户事业部Id
    var curOrgId = [[${teleGroupId}]]; // 当前用户电销组id
    var curUserId = [[${teleSaleId}]]; // 当前用户顾问Id
    var mainDivVM = new Vue({
        el: '#maindiv',
        data: function() {
            return {
                isElBreadcrumb:true,
                loading: false,
                isDisabled: false,
                isDisabledOrgId: false,
                isDisabledUserId: false,
                pager:{
                    total: 0,
                    currentPage: 1,
                    pageSize: 20,
                },
                form: {
                    startTime:'',
                    endTime:'',
                    teleDeptId: '',
                    teleGroupId: '',
                    teleSaleId: '',
                    category:'',
                    searchText: ''
                },
                export: {
                    startTime:'',
                    endTime:'',
                    teleDeptId: curDeptId,
                    teleGroupId: curOrgId,
                    teleSaleId: '',
                    category:'',
                    searchText: ''
                },
                tableData: [],
                deptList: deptList,// 电销事业部
                orgTeleArr: '', // 电销组
                busSaleArr: '', // 电销顾问
                clueCategoryList: clueCategoryList, // 资源类别
            }             
        },
        methods: {
            format_number(row, column){
                var nStr = row[column.property];
                if('--'==nStr){
                    return nStr;
                }
                nStr += '';  
                x = nStr.split('.');  
                x1 = x[0];
                if(!x[1]){
                    x[1] = '00';
                }  
                x2 = x.length > 1 ? '.' + x[1] : '';
                var rgx = /(\d+)(\d{3})/;  
                while (rgx.test(x1)) {  
                    x1 = x1.replace(rgx, '$1' + ',' + '$2');  
                }  
                return x1 + x2;  
            },    
            centerStyle(){
                return "text-align:center"
            },
            firstCurTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelSale(seleVal);
            },
            curTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.teleSaleId = '';
                this.getTelSale(seleVal);
            },
            firstCurTelGroupSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelGroupSale(seleVal);
            },
            curTelGroupSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.teleGroupId = '';
                this.form.teleSaleId = '';
                this.getTelGroupSale(seleVal);
            },
            getTelGroupSale(seleVal){
                param={};
                param.parentId=seleVal;
                param.orgType=1;
                axios.post('/base/getGroupList',param).then(function (response) {
                    mainDivVM.orgTeleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "电销组请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            getTelSale(seleVal){
                param={};
                param.orgId=seleVal;
                param.roleCode="DXCYGW";
                axios.post('/base/getSaleList' ,param).then(function (response) {  
                    mainDivVM.busSaleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "电销顾问请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            searchTable(firstPage) { 
                var startTime = this.form.startTime;
                var endTime = this.form.endTime;
                if(endTime && startTime){
                    startTime = new Date(this.form.startTime);
                    endTime = new Date(this.form.endTime);
                    if(startTime>endTime){
                        this.$message({
                            message: '开始时间不能大于结束时间',
                            type: 'warning'
                        });
                        return;
                    }
                    var date = new Date();
                    var currentDate = moment(date).format("YYYY-MM-DD 23:59");
                    // date.setMonth(date.getMonth()-6);
                    // var beforeSixMonth = moment(date).format("YYYY-MM-DD 00:00");
                    var currentDateRes = new Date(currentDate);
                    // var beforeSixMonthRes = new Date(beforeSixMonth);
                    if(endTime > currentDateRes){
                        this.$message({
                            message: '请选择当前日期前的时间',
                            type: 'warning'
                        });
                        return;
                    }
                    startTime = this.limitTime(startTime, null);
                    endTime = this.limitTime(null, endTime);
                }else{
                    this.$message({
                        message: '请选择起止时间',
                        type: 'warning'
                    });
                    return;
                }   
    
                var param = {};
                param.startTime = this.minuteFormat(startTime);
                param.endTime = this.minuteFormat(endTime);
                param.pageSize = this.pager.pageSize;
    
                if(firstPage){
                    param.pageNum = firstPage;
                }else{
                    param.pageNum = this.pager.currentPage;
                }
                param.teleDeptId = this.form.teleDeptId;
                param.teleGroupId = this.form.teleGroupId;
                param.teleSaleId = this.form.teleSaleId;
                param.category = this.form.category;
                param.searchText = this.form.searchText;
                this.loading = true;
                axios.post('/performance/querySalePage',param).then(function (response) {
                    if(!response || !response.data){
                        mainDivVM.$message({
                            message: "接口调用失败",
                            type: 'error'
                        });
                        return ;
                    }
                    var resobj= response.data;
                    if(resobj.code != '0'){
                        mainDivVM.loading = false;
                        mainDivVM.$message({
                            message: resobj.msg,
                            type: 'error'
                        });
                        return ;
                    }else if(resobj.data && resobj.data.tableData){
                        var resobjData = resobj.data;
                        var tableData = resobjData.tableData;
                        mainDivVM.tableData = tableData.data;
                        if( tableData.data.length && resobj.data.totalData ){
                            var totalData = resobjData.totalData[0];
                            mainDivVM.tableData.unshift(totalData);
                        }
                        mainDivVM.pager.currentPage = tableData.currentPage;
                        mainDivVM.pager.total = tableData.total;
                        mainDivVM.loading = false;
                        mainDivVM.export.startTime = mainDivVM.minuteFormat(startTime);
                        mainDivVM.export.endTime = mainDivVM.minuteFormat(endTime);
                        mainDivVM.export.teleDeptId = mainDivVM.form.teleDeptId;
                        mainDivVM.export.teleGroupId = mainDivVM.form.teleGroupId;
                        mainDivVM.export.teleSaleId = mainDivVM.form.teleSaleId;
                        mainDivVM.export.category = mainDivVM.form.category;
                        mainDivVM.export.searchText = mainDivVM.form.searchText;
        
                    }else{
                        mainDivVM.loading = false;
                    }
                }).catch(function (error) {
                    mainDivVM.loading = false;
                    mainDivVM.$message({
                        message: "请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            limitTime (start, end) { // 禁止选择分钟
                var endTime = this.minuteFormat (end);
                var startTime = this.minuteFormat (start);
                var date = new Date();
                data = this.minuteFormat (date);// 现在的时间
                data = data.slice(0,8);
                if(startTime){
                    this.form.startTime = moment(start).format("YYYY-MM-DD 00:00");
                    return this.form.startTime;
                }
                if(endTime){
                    this.form.endTime = moment(end).format("YYYY-MM-DD 23:59");
                    return this.form.endTime;   
                }
            },
            goBack () {
                window.history.go(-1);
            },
            arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合并第一行
                if (rowIndex === 0) {
                    if (columnIndex === 0) {
                        return [0, 0];
                    } else if (columnIndex === 1) {
                        return [1, 2];
                    }
                }
            },
            indexFun (index) { // 表格序号
                return index;
            },
            percentFormat(row, column) {//百分比格式化
                var percent = row[column.property];
                if (!percent) { // 如果没有值或值为0
                    return "0%";
                }
                percent = (percent * 1000) / 10 + "%";
                return percent;
            },
            dateTimeFormat (dt) {
                if (!dt) {return "";}
                var d = dt.toString();
                var oYear = d.slice(0,4);
                var oMonth = d.slice(4,6);
                var oDay = d.slice(6,8);
                var oHour = d.slice(8,10);
                var oMinute = d.slice(10);
                return oYear+"-"+oMonth+"-"+oDay+" "+oHour+":"+oMinute;
            },
            minuteFormat (m) {
                if (!m) {return "";}
                var timeStr = new Date(m);
                var yearRes = timeStr.getFullYear()
                var monthRea = timeStr.getMonth()+1;
                monthRea = monthRea < 10 ?  "0" + monthRea : monthRea;
                var dateRes = timeStr.getDate();
                dateRes = dateRes < 10 ?  "0" + dateRes : dateRes;  
                var hoursRes = timeStr.getHours();
                hoursRes = hoursRes < 10 ?  "0" + hoursRes : hoursRes;  
                var minRes = timeStr.getMinutes();
                minRes = minRes < 10 ?  "0" + minRes : minRes;                
                var timeRes = "" + yearRes + monthRea + dateRes + hoursRes + minRes;
                return timeRes;
            },
            searchSeason(){ // 季
                var today = new Date();
                var month = today.getMonth()+1;
                var startTime = '';
                switch (month) {
                    case 1 : 
                    case 2 : 
                    case 3 : startTime = today.getFullYear()+"-" + 01 + "-" + 01+" 00:00";break;
                    case 4 : 
                    case 5 : 
                    case 6 : startTime = today.getFullYear()+"-" + 04 + "-" + 01+" 00:00"; break;
                    case 7 : 
                    case 8 : 
                    case 9 : startTime = today.getFullYear()+"-" + 07 + "-" + 01+" 00:00";break;
                    case 10 :
                    case 11 :
                    case 12 : startTime = today.getFullYear()+"-" + 10 + "-" + 01+" 00:00";break;
                }
               var endTime = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 23:59";
               this.form.startTime=startTime;
               this.form.endTime=endTime;
               this.searchTable(1);
            },
            searchYear(){ // 年
               var today = new Date();
               var startTime = today.getFullYear()+"-" + 01 + "-" + 01+" 00:00";
               var endTime = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 23:59";
               this.form.startTime=startTime;
               this.form.endTime=endTime;
               this.searchTable(1);
            },
            searchMonth(){ // 本月 
                var a = new Date();
                var year = a.getFullYear();
                var month = a.getMonth();
                var date = a.getDate();
                var b = new Date(year,month,date);
                var f= new Date(year,month,01);
                var c = f.valueOf();
                var startTime= new Date(c);
                this.form.startTime=startTime.getFullYear()+"-" + (startTime.getMonth()+1) + "-" + startTime.getDate()+" 00:00";
                this.form.endTime=year+"-"+(month+1)+"-"+date+" 23:59";
                this.searchTable(1);
            },
            exportRs(){
                var param = {};
                param.startTime = this.export.startTime;
                param.endTime = this.export.endTime;
                param.teleDeptId =  this.export.teleDeptId;
                param.teleGroupId = this.export.teleGroupId;
                param.teleSaleId = this.export.teleSaleId;
                param.category = this.export.category;
                param.searchText = this.export.searchText;
                axios.post('/performance/saleExport',param,{responseType:'blob'})
                .then(function (response) {
                    var data =  response.data;
                    var fileName = response.headers.filename;
                    saveAs(data,decodeURI(fileName));
                })
                .catch(function (error) {
                    mainDivVM.$message({
                        message: "导出失败，请稍后重试",
                        type: "error"
                    });
                });
            }
        },
        created(){     
            var today = new Date();
            var startTime = moment(today).format("YYYY-MM-DD 00:00");
            var endTime = moment(today).format("YYYY-MM-DD 23:59");
            if(curDeptId){
                this.isDisabled = true;
                this.form.teleDeptId = curDeptId;
                // this.firstCurTelGroupSale(curDeptId);
                if(curOrgId){ // 有角色
                    this.isDisabledOrgId = true;
                    this.form.teleGroupId = curOrgId;
                    // this.firstCurTelSale(curOrgId);
                    if(curUserId){
                        this.isDisabledUserId = true;
                        this.form.teleSaleId = curUserId;
                    }
                    // else{
                    //     this.form.teleSaleId = parentParam&&parentParam.teleSaleId?parentParam.teleSaleId:'';
                    // }
                }else{
                    this.form.teleGroupId = parentParam&&parentParam.teleGroupId ? parentParam.teleGroupId:'';
                }
            }else{
                this.form.teleDeptId = parentParam&&parentParam.teleDeptId ? parentParam.teleDeptId:'';
                this.form.teleGroupId = parentParam&&parentParam.teleGroupId ? parentParam.teleGroupId:'';
                // this.form.teleSaleId = parentParam&&parentParam.teleSaleId ? parentParam.teleSaleId:'';
            }
            if(parentParam && parentParam.startTime){
                this.form.startTime = this.dateTimeFormat(parentParam.startTime);
                this.form.endTime = this.dateTimeFormat(parentParam.endTime);

                this.export.startTime = parentParam.startTime;
                this.export.endTime = parentParam.endTime;
            }else{
                this.form.startTime = startTime;
                this.form.endTime = endTime;
                this.export.startTime = this.minuteFormat(this.form.startTime);
                this.export.endTime = this.minuteFormat(this.form.endTime);
            }
            this.form.category = parentParam&&parentParam.category ? parentParam.category:''; 
            this.form.searchText = parentParam&&parentParam.searchText ? parentParam.searchText:''; 
            this.export.teleDeptId = this.form.teleDeptId;
            this.export.teleGroupId = this.form.teleGroupId;
            this.export.teleSaleId = this.form.teleSaleId;
            this.export.category = this.form.category;
            this.export.searchText = this.form.searchText;

            this.firstCurTelGroupSale(this.form.teleDeptId);
            this.firstCurTelSale(this.form.teleGroupId);

            var localVal=localStorage.getItem('allChangePageSize')?parseInt(localStorage.getItem('allChangePageSize')):'';
            if(localVal){this.pager.pageSize = localVal;}

            this.searchTable();
            // 判断是否显示面包屑
            var type=getQueryString("type");
            console.log(type)
            if(type=="1"){
                // 隐藏面包屑
                this.isElBreadcrumb=false;                
            }
        },
        mounted(){
            document.getElementById('maindiv').style.display = 'block';
        }
    })
    
</script>
</html>